package com.wu.yygh.cmn.controller;

import com.wu.yygh.cmn.service.DictService;
import com.wu.yygh.common.result.Result;
import com.wu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName DictController
 * @Author 浮生光
 * @Date 2022/7/9 23:06
 * @Description DictController
 * @Version 1.0
 */
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;


    /**
     * 导入数据字典
     * @param file 上传的文件
     */
    @ApiOperation("导入数据字典")
    @PostMapping("/importData")
    public Result importDict(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }


    /**
     * 导出数据字典接口
     * @param response 响应
     */
    @ApiOperation("导出数据字典接口")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }


    /**
     * 根据数据ID查询子数据列表
     */
    @ApiOperation("根据数据ID查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> dictList = dictService.findChildDataById(id);
        return Result.ok(dictList);
    }
    /**
     * 根据dictcode和value值进行查询字典名字
     */
    @ApiOperation("根据dictcode和value值进行查询字典名字")
    @GetMapping("/getName/{dictCode}/{value}")
    public Result getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String name = dictService.getName(dictCode, value);
        return Result.ok(name);
    }


    /**
     * 根据value进行查询
     */
    @ApiOperation("根据value进行查询")
    @GetMapping("/getName/{value}")
    public Result getName(@PathVariable String value) {
        String name = dictService.getName(null, value);
        return Result.ok(name);
    }



}
